<template>
  <div>
    <Bread></Bread>

    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-input
            placeholder="请输入内容"
            class="input-with-select"
            v-model="obj.query"
            clearable
            @clear="getData"
          >
            <el-button slot="append" icon="el-icon-search" @click="getData"></el-button>
          </el-input>
        </el-col>
        <el-col :span="8">
          <el-button type="primary" @click="dialogVisible = true">添加用户</el-button>
        </el-col>
      </el-row>
      <el-table :data="data.users" border style="width: 100%">
        <el-table-column fixed type="index" label="#" width="100"></el-table-column>
        <el-table-column prop="username" label="姓名" width="150"></el-table-column>
        <el-table-column prop="email" label="邮箱" width="250"></el-table-column>
        <el-table-column prop="mobile" label="电话" width="120"></el-table-column>
        <el-table-column prop="role_name" label="角色" width="120"></el-table-column>
        <el-table-column prop="mg_state" label="状态" width="160">
          <template v-slot="scope">
            <el-switch v-model="scope.row.mg_state" @change="savestate(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="130">
          <template v-slot="scop">
            <el-button type="info" icon="el-icon-message" circle @click="enditData(scop.row)"></el-button>
            <el-button type="warning" icon="el-icon-star-off" circle></el-button>
            <el-button type="danger" icon="el-icon-delete" circle @click="del(scop.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next "
        :total="data.total"
        :current-page="obj.pagenum"
        :page-size="obj.pagesize"
        @current-change="getNowpage"
      ></el-pagination>
    </el-card>

    <!-- //添加数据 -->
    <el-dialog title="收货地址" :visible.sync="dialogVisible">
      <el-form :model="addDataobj" :rules="addRules" ref="addfrom">
        <el-form-item label="用户名" label-width="120px" prop="username">
          <el-input autocomplete="off" v-model="addDataobj.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="120px" prop="password">
          <el-input autocomplete="off" v-model="addDataobj.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" label-width="120px" prop="email">
          <el-input autocomplete="off" v-model="addDataobj.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" label-width="120px" prop="mobile">
          <el-input autocomplete="off" v-model="addDataobj.mobile"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addData">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 编辑数据 -->
    <el-dialog title="编辑数据" :visible.sync="enditVisible">
      <el-form :model="enditshuju" :rules="enditrules" ref="enditref">
        <el-form-item label="用户名" label-width="120px" prop="username">
          <el-input autocomplete="off" v-model="enditshuju.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="邮箱" label-width="120px" prop="email">
          <el-input autocomplete="off" v-model="enditshuju.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" label-width="120px" prop="mobile">
          <el-input autocomplete="off" v-model="enditshuju.mobile"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="enditVisible = false">取 消</el-button>
        <el-button type="primary" @click="getEndit(enditshuju.id)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Bread from "@/components/breadmenu.vue";
export default {
  components: { Bread },
  data() {
    //邮箱验证格式
    var checkEmail = (rule, value, callback) => {
      var reg = /^[A-Za-z0-9]+([_\.][A-Za-z0-9]+)*@([A-Za-z0-9\-]+\.)+[A-Za-z]{2,6}$/;
      if (!reg.test(value)) {
        return callback(new Error("请输入正确的邮箱格式"));
      } else {
        callback();
      }
    };
    return {
      obj: {
        query: "",
        pagenum: 1,
        pagesize: 3
      },
      data: {},
      value: true,
      dialogVisible: false, //添加对话框打开还是关闭,
      enditVisible: false, //添加编辑对话框打开还是关闭,

      //添加对话框的验证规则
      addRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入正确的密码", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" }
        ],
        email: [
          { required: true, message: "请输入你的邮箱", trigger: "blur" },
          { validator: checkEmail, trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "请输入正确的手机号", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" }
        ]
      },
      // 添加编辑对话框的验证规则
      enditrules: {
        email: [
          { required: true, message: "请输入你的邮箱", trigger: "blur" },
          { validator: checkEmail, trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "请输入正确的手机号", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" }
        ]
      },
      //添加数据对象
      addDataobj: {
        username: "",
        password: "",
        email: "",
        mobile: ""
      },
      // 编辑数据
      enditshuju: {}
    };
  },
  methods: {
    async getData() {
      var res = await this.$http.get(
        "/users?pagenum=" +
          this.obj.pagenum +
          "&pagesize=" +
          this.obj.pagesize +
          "&query=" +
          this.obj.query
      );
      console.log(res);
      this.data = res.data.data;
    },

    getNowpage(nowpage) {
      this.obj.pagenum = nowpage;
      this.getData();
    },
    ///添加数据
    addData() {
      this.$refs.addfrom.validate(async vaild => {
        console.log(vaild);
        if (vaild) {
          //提交接口的数据
          var { data } = await this.$http.post("/users", this.addDataobj);
          console.log(data);
          if (data.meta.status == 201) {
            this.dialogVisible = false;
            this.getData();
          }
        }
      });
    },
    async savestate(a) {
      console.log(a);
      var res = await this.$http.put(`users/${a.id}/state/${a.mg_state}`);
      console.log(res);
      if (res.data.meta.status == 200) {
        this.$message({
          message: "状态设置成功",
          type: "success"
        });
      }
    },

    del(id) {
      // var res = await this.$http.delete("users/" + id);
      // console.log(res);
      this
        .$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
        .then(async () => {
          var res = await this.$http.delete("users/" + id);
          console.log(res);
          if (res.data.meta.status == 200) {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.getData();
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 编辑
    enditData(valuea) {
      console.log(valuea);
      this.enditVisible = true; //编辑框的显示
      this.enditshuju = valuea;
    },

    // 编辑提交用户数据
    getEndit(id) {
      //当数据效验成功后调用接口
      this.$refs.enditref.validate(async value => {
        if (value) {
          var obj = {
            email: this.enditshuju.email,
            mobile: this.enditshuju.mobile
          };
          // console.log(obj)
          var { data: res } = await this.$http.put("users/" + id,obj);
          // console.log(res)
          if (res.meta.status == 200) {
            this.$message({
              type: "success",
              message: "编辑成功!"
            });
            this.enditVisible = false;
            this.getData();
            
          }
        }
      });
    }
  },
  mounted() {
    this.getData();
  }
};
</script>
<style lang="scss" scoped>
.el-card {
  margin-top: 15px;
}
.el-table {
  margin-top: 15px;
}
.el-dialog {
  p {
    display: flex;
    align-items: center;
    span {
      color: red;
    }
    .el-input {
      margin-bottom: 6px;
      margin-left: 15px;
      width: 200px;
    }
  }
}
</style>
